﻿using System;
using WebAndLoadTestPluginsAndRules.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace HtmlTagInnerTextTests
{
    /// <summary>
    ///This is a test class for QueryStringParameterCollectionExtensionsTest and is intended
    ///to contain all QueryStringParameterCollectionExtensionsTest Unit Tests
    ///</summary>
    [TestClass()]
    public class QueryStringParameterCollectionExtensionsTest
    {
        /// <summary>
        ///A test for AddQueryStringParameter
        ///</summary>
        [TestMethod]
        [Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItem("WebAndLoadTestPluginsAndRules.dll")]
        public void AddQueryStringParameter_WhenParameterDoesntExist_EnsureThatParameterIsAdded()
        {
            // Arrange
            const string parameterName = "Name";
            const string parameterValue = "Value";
            const bool overwriteIfExists = false;
            QueryStringParameterCollection queryStringParameters = new QueryStringParameterCollection();
            QueryStringParameterCollection expectedParameters = new QueryStringParameterCollection { { parameterName, parameterValue } };

            // Act
            queryStringParameters.AddQueryStringParameter(parameterName, parameterValue, overwriteIfExists);

            // Assert
            AssertHelpers.AssertCollectionsEqual(expectedParameters, queryStringParameters);
        }


        /// <summary>
        ///A test for AddQueryStringParameter
        ///</summary>
        [TestMethod]
        [Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItem("WebAndLoadTestPluginsAndRules.dll")]
        public void AddQueryStringParameter_WhenParameterExistsAndNotOverwriting_EnsureThatParameterIsNotOverwritten()
        {
            // Arrange
            const string parameterName = "Name";
            const string parameterNewValue = "NewValue";
            const string parameterOldValue = "OldValue";
            const bool overwriteIfExists = false;
            QueryStringParameterCollection queryStringParameters = new QueryStringParameterCollection { { parameterName, parameterOldValue } };
            QueryStringParameterCollection expectedParameters = new QueryStringParameterCollection { { parameterName, parameterOldValue } };

            // Act
            queryStringParameters.AddQueryStringParameter(parameterName, parameterNewValue, overwriteIfExists);

            // Assert
            AssertHelpers.AssertCollectionsEqual(expectedParameters, queryStringParameters);
        }
        /// <summary>
        ///A test for AddQueryStringParameter
        ///</summary>
        [TestMethod]
        [Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItem("WebAndLoadTestPluginsAndRules.dll")]
        public void AddQueryStringParameter_WhenParameterExistsAndOverwriting_EnsureThatParameterIsOverwritten()
        {
            // Arrange
            const string parameterName = "Name";
            const string parameterNewValue = "NewValue";
            const string parameterOldValue = "OldValue";
            const bool overwriteIfExists = true;
            QueryStringParameterCollection queryStringParameters = new QueryStringParameterCollection { { parameterName, parameterOldValue } };
            QueryStringParameterCollection expectedParameters = new QueryStringParameterCollection { { parameterName, parameterNewValue } };

            // Act
            queryStringParameters.AddQueryStringParameter(parameterName, parameterNewValue, overwriteIfExists);

            // Assert
            AssertHelpers.AssertCollectionsEqual(expectedParameters, queryStringParameters);
        }
        /// <summary>
        ///A test for AddQueryStringParameter
        ///</summary>
        [TestMethod]
        [Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItem("WebAndLoadTestPluginsAndRules.dll")]
        public void AddQueryStringParameter_WhenQueryStringParametersNull_EnsureThrowsArgumentException()
        {
            // Arrange
            const string parameterName = "Name";
            const string parameterNewValue = "NewValue";
            const bool overwriteIfExists = true;
            const QueryStringParameterCollection queryStringParameters = null;

            // Act
            Action action = () => queryStringParameters.AddQueryStringParameter(parameterName, parameterNewValue, overwriteIfExists);

            // Assert
            AssertHelpers.ThrowsArgumentException<ArgumentNullException>(action, "queryStringParameters");
        }
        /// <summary>
        ///A test for AddQueryStringParameter
        ///</summary>
        [TestMethod]
        [Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItem("WebAndLoadTestPluginsAndRules.dll")]
        public void AddQueryStringParameter_WhenParameterNameNull_EnsureThrowsArgumentException()
        {
            // Arrange
            const string parameterName = "Name";
            const string parameterOldValue = "OldValue";
            const string parameterNewValue = "NewValue";
            const bool overwriteIfExists = true;
            QueryStringParameterCollection queryStringParameters = new QueryStringParameterCollection { { parameterName, parameterOldValue } };

            // Act
            Action action = () => queryStringParameters.AddQueryStringParameter(null, parameterNewValue, overwriteIfExists);

            // Assert
            AssertHelpers.ThrowsArgumentException<ArgumentException>(action, "parameterName");
        }
        /// <summary>
        ///A test for AddQueryStringParameter
        ///</summary>
        [TestMethod]
        [Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItem("WebAndLoadTestPluginsAndRules.dll")]
        public void AddQueryStringParameter_WhenParameterNameEmpty_EnsureThrowsArgumentException()
        {
            // Arrange
            const string parameterName = "Name";
            const string parameterOldValue = "OldValue";
            const string parameterNewValue = "NewValue";
            const bool overwriteIfExists = true;
            QueryStringParameterCollection queryStringParameters = new QueryStringParameterCollection { { parameterName, parameterOldValue } };

            // Act
            Action action = () => queryStringParameters.AddQueryStringParameter("", parameterNewValue, overwriteIfExists);

            // Assert
            AssertHelpers.ThrowsArgumentException<ArgumentException>(action, "parameterName");
        }
    }
}